package com.seaboxdata.threadHandler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.GpsRecord;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author
 * @create 2021-05-25 11:37
 **/
@Component
@Slf4j
public class GpsRecordHandler implements MessageListener {

    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;

    @Value("${jfxt.gpsRecord.topic}")
    private String gpsRecordTopic;

    @Override
    public void onMessage(Message message) {
        try {
            JSONArray results = JSON.parseArray(((TextMessage) message).getText());
            if (results != null && !results.isEmpty()) {
                Iterator<Object> iterator = results.iterator();
                while (iterator.hasNext()) {
                    GpsRecord gpsRecord = ((JSONObject) iterator.next()).toJavaObject(GpsRecord.class);
                    gpsRecord.setCjsj(new Date());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    SimpleDateFormat sdfOld = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                    gpsRecord.setGpsTime(sdf.format(sdfOld.parse(gpsRecord.getGpsTime())));
                    baseStringKafkaProducer.sendMessageToKafka(JSONObject.toJSONString(gpsRecord), gpsRecordTopic);
                }
            }
        } catch (Exception e) {
            log.error("gpsRecord:get message error");
            e.printStackTrace();
        }
    }

}
